home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / ULARN.ARJ / ULARN.TAR / ularn / tok.c < prev    next >
C/C++ Source or Header  |  1989-10-25  |  6KB  |  260 lines

  1. /* tok.c */
  2. #include "header.h"
  3.  
  4. #define FLUSHNO 5
  5. #define MAXUM 52    /* maximum number of user re-named monsters */
  6. #define MAXMNAME 40    /* max length of a monster re-name */
  7.  
  8. static char lastok=0;
  9. int yrepcount=0,dayplay=0;
  10. #ifdef BSD
  11. static int flushno=FLUSHNO;    /* input queue flushing threshold */
  12. #endif /* BSD */
  13. static char usermonster[MAXUM][MAXMNAME]; 
  14. static char usermpoint=0;            /* the user monster pointer */
  15.  
  16. /*
  17.     lexical analyzer for larn
  18.  */
  19. yylex()
  20. {
  21.     char cc;
  22.     int i, ic;
  23.  
  24.     if (hit2flag) { 
  25.         hit2flag=0;  
  26.         yrepcount=0;  
  27.         return(' '); 
  28.     }
  29.     if (yrepcount>0)    { 
  30.         --yrepcount;  
  31.         return(lastok);    
  32.     } 
  33.     else yrepcount=0;
  34.  
  35.     /*show where the player is*/
  36.     if (yrepcount==0) { 
  37.         bottomdo(); 
  38.         showplayer(); 
  39.     }    
  40.     lflush();  
  41.     while (1) {
  42.         c[BYTESIN]++;
  43.         if (ckpflag)
  44.             /* check for periodic checkpointing */
  45.             if ( c[BYTESIN] == 1 || (c[BYTESIN] % 400) == 0) {
  46.                 wait((int *)0);/* wait for other forks to finish */
  47.                 if (fork() == 0) { 
  48.                     savegame(ckpfile); 
  49.                     exit(0); 
  50.                 }
  51.             }
  52. #ifdef BSD
  53.         /* if keyboard input buffer is too big, flush some of it */
  54.         do {
  55.             ioctl(0,FIONREAD,&ic);
  56.             if (ic>flushno)   
  57.                 read(0,&cc,1);
  58.         }
  59.         while (ic>flushno);
  60. #endif
  61.         if (read(0,&cc,1) != 1) 
  62.             return(lastok = -1);
  63.  
  64.         if (cc == '!')    /* shell escape */
  65.         {     
  66.             resetscroll();  
  67.             clear();
  68.             sncbr();
  69.             cl_dn(0,0);
  70.             lflush();
  71.             if ((ic=fork())==0) {
  72.                 execl("/bin/csh", "/bin/csh", (char *)0);    
  73.                 exit(1);
  74.             }
  75.             do { 
  76.                 i = wait(0);
  77.             } while (i != ic && i != -1);
  78.  
  79.             if (ic<0) {     
  80.                 fprintf(stderr,"Can't fork /bin/csh!\n"); 
  81.                 fflush(stderr);
  82.                 sleep(2);
  83.             }
  84.             setscroll();
  85.             scbr();
  86.             return(lastok = 'L'-64);    /* redisplay screen */
  87.         }
  88.         if ((cc <= '9') && (cc >= '0')) 
  89.             yrepcount = yrepcount*10 + cc - '0'; 
  90.         else {    
  91.             if (yrepcount>0) --yrepcount;  
  92.             return(lastok = cc); 
  93.         }
  94.     }
  95. }
  96.  
  97. /*
  98.  *    flushall()    Function to flush all type-ahead in the input buffer
  99.  */
  100. flushall()
  101. {
  102. #ifdef BSD
  103.     char cc;
  104.     int ic;
  105.  
  106.     for (;;)/* if keyboard input buffer is too big, flush some of it */
  107.     {
  108.         ioctl(0,FIONREAD,&ic);
  109.         if (ic<=0) return;
  110.         while (ic>0)   { 
  111.             read(0,&cc,1); 
  112.             --ic; 
  113.         } /* gobble up the byte */
  114.     }
  115. #else /* SYSV */
  116. #  ifdef unix
  117.     tcflush(0, TCIFLUSH);        /* unix SYSV only, not XENIX */
  118. #  endif /* unix */
  119. #endif /* BSD */
  120. }
  121.  
  122. /*
  123.     function to set the desired hardness 
  124.     enter with hard= -1 for default hardness, else any desired hardness
  125.  */
  126. sethard(hard)
  127. int hard;
  128. {
  129.     register int j,k,i;
  130.  
  131.     if (restorflag==0)  {      /* don't set c[HARDGAME] if restoring game */
  132.         if (hashewon() == 0) {
  133.             if (hard >= 0) 
  134.                 c[HARDGAME] = hard;
  135.         }
  136.         else  if (hard > c[HARDGAME]) c[HARDGAME] = hard;
  137.     }
  138.  
  139.     if (k=c[HARDGAME])
  140.         for (j=0; j<=MAXMONST+8; j++) {
  141.             i = ((6+k)*monster[j].hitpoints+1)/6;
  142.             monster[j].hitpoints = (i<0) ? 32767 : i;
  143.             i = ((6+k)*monster[j].damage+1)/5;
  144.             monster[j].damage = (i>127) ? 127 : i;
  145.             i = (10*monster[j].gold)/(10+k);
  146.             monster[j].gold = (i>32767) ? 32767 : i;
  147.             i = monster[j].armorclass - k;
  148.             monster[j].armorclass = (i< -127) ? -127 : i;
  149.             i = (7*monster[j].experience)/(7+k) + 1;
  150.             monster[j].experience = (i<=0) ? 1 : i;
  151.         }                
  152. }
  153.  
  154. /*
  155.     function to read and process the larn options file
  156.  */
  157. readopts()
  158. {
  159.     register char *i;
  160.     register int j,k;
  161.     extern int char_picked;
  162.     int flag=1;
  163.  
  164.     if (lopen(optsfile) < 0) {
  165.         strcpy(logname,loginname); 
  166.         return; /* user name if no character name */
  167.     }
  168.     i = " ";
  169.     while (*i) {
  170.         if ((i=(char *)lgetw()) == 0) break; /* check for EOF */
  171.         while ((*i==' ') || (*i=='\t')) i++; /* eat leading whitespace*/
  172.         switch(*i) {
  173.         case 'b':    
  174.             if (strcmp(i,"bold-off") == 0)  
  175.                 boldon=0;
  176.             break;
  177.  
  178.         case 'e':    
  179.             if (strcmp(i,"enable-checkpointing") == 0) 
  180.                 ckpflag=1;
  181.             break;
  182.  
  183.         case 'f':    
  184.             if (strcmp(i,"female") == 0)    
  185.                 sex=0; /* male or female */
  186.             break;
  187.  
  188.             /* name favorite monster */
  189.         case 'm':    
  190.             if (strcmp(i,"monster:")== 0)   {
  191.                 if ((i=lgetw())==0) break;
  192.                 if (strlen(i)>=MAXMNAME) i[MAXMNAME-1]=0;
  193.                 strcpy(usermonster[usermpoint],i);
  194.                 /* defined all of em */
  195.                 if (usermpoint >= MAXUM) break; 
  196.                 if (isalpha(j=usermonster[usermpoint][0])) {
  197.                     for (k=1; k<MAXMONST+8; k++)
  198.                         if (monstnamelist[k] == j) {
  199.                 monster[k].name = &usermonster[usermpoint++][0];
  200.                             break;
  201.                         }
  202.                 }
  203.             }
  204.             else if (strcmp(i,"male") == 0)    sex=1;
  205.             break;
  206.  
  207.         case 'n':    
  208.             if (strcmp(i,"name:") == 0) {
  209.                 if ((i=lgetw())==0) break;
  210.                 if (strlen(i)>=LOGNAMESIZE) 
  211.                     i[LOGNAMESIZE-1]=0;
  212.                 strcpy(logname,i); 
  213.                 flag=0;
  214.             }
  215.             else if (strcmp(i,"no-introduction") == 0) 
  216.                 nowelcome=1;
  217.             else if (strcmp(i,"no-beep") == 0) 
  218.                 nobeep=1;
  219.             break;
  220.  
  221.         case 'c':
  222.             if (strncmp(i,"character:",strlen("character:")) == 0) {
  223.                 int k=0, j=0;
  224.                 j = strlen("character:");
  225.                 do { 
  226.                     char_class[k++] = i[j]; 
  227.                 } while (i[j++] != '\0');
  228.                 flag = 0;
  229.                 if (!strcmp(char_class, "ogre"))
  230.                     char_picked = 'a';
  231.                 else if (!strcmp(char_class, "wizard"))
  232.                     char_picked = 'b';
  233.                 else if (!strcmp(char_class, "klingon"))
  234.                     char_picked = 'c';
  235.                 else if (!strcmp(char_class, "elf"))
  236.                     char_picked = 'd';
  237.                 else if (!strcmp(char_class, "rogue"))
  238.                     char_picked = 'e';
  239.                 else if (!strcmp(char_class, "geek"))
  240.                     char_picked = 'f';
  241.                 else if (!strcmp(char_class, "dwarf"))
  242.                     char_picked = 'g';
  243.                 else if (!strcmp(char_class, "rambo"))
  244.                     char_picked = 'h';
  245.             }
  246.             break;
  247.         case 's':    
  248.             if (strcmp(i,"savefile:") == 0) {
  249.                 if ((i=lgetw())==0) break;
  250.                 if (strlen(i)>=SAVEFILENAMESIZE)
  251.                     i[SAVEFILENAMESIZE-1]=0;
  252.                 strcpy(savefilename,i); 
  253.                 flag=0;
  254.             }
  255.             break;
  256.         };
  257.     }
  258.     if (flag)  strcpy(logname,loginname);
  259. }
  260.